<template>
	<view class="mine-coupon-list">
		<fixed>
			<tab @change="handerTabChange" :list="tabList" :activeIndex="activeIndex" :activeColor="primaryColor"
				:width="100/tabList.length + '%'" height="100rpx"></tab>
			<view class="b-1px-b"></view>
		</fixed>

		<view class="list-item fill-base mg-big pd-lg radius-34" v-for="(item,index) in list.data" :key="index">
			<view class="flex-between">
				<view class="flex-center flex-column">
					<view class="flex-y-baseline f-caption c-warning">¥<view class="f-lg-title">{{item.discount}}
						</view>
					</view>
					<view class="f-caption c-title">优惠券</view>
				</view>
				<view class="flex-1 ml-lg flex-between">
					<view class="f-title c-title mr-lg">
						<view class="text-bold">{{item.title}}</view>
						<view class="f-icontext mt-sm">{{item.type==0?`满${item.full}元可用`:'无门槛'}}</view>
						<view @tap.stop="this.list.data[index].is_show = !this.list.data[index].is_show"
							class="flex-y-baseline f-icontext mt-sm">查看详情
							<i class="iconfont ml-sm" style="font-size: 24rpx"
								:class="[{'icon-down':!item.is_show},{'icon-up':item.is_show}]"></i>
						</view>
					</view>
					<view @tap.stop="goDetail(index)" class="use-btn flex-center f-caption c-base radius"
						:style="{background:activeIndex==0?primaryColor:activeIndex==1?'#aaa':'#F1381F'}">
						{{statusType[item.status]}}
					</view>
				</view>
			</view>
			<view class="mt-lg pd-lg f-icontext c-title radius-16" style="background: #F9FAF9" v-if="item.is_show">
				<view class="flex-warp">
					<view>卡券数量：</view>
					<view class="flex-1 c-desc">x{{item.num}}</view>
				</view>
				<view class="flex-warp mt-md">
					<view>使用时间：</view>
					<view class="flex-1 c-desc">{{activeIndex==1?item.use_time : item.start_time}}</view>
				</view>
				<view class="flex-warp mt-md">
					<view>使用范围：</view>
					<view class="flex-1 c-desc">{{item.use_range}}</view>
				</view>
				<view class="flex-warp mt-md">
					<view>使用规则：</view>
					<view class="flex-1 c-desc">
						<text decode="emsp" style="word-break:break-all;">{{item.rule}}</text>
					</view>
				</view>
				<view class="flex-warp mt-md">
					<view>优惠详情：</view>
					<view class="flex-1 c-desc">
						<text decode="emsp" style="word-break:break-all;">{{item.text}}</text>
					</view>
				</view>
			</view>
		</view>

		<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0" :loading="loading" v-if="loading">
		</load-more>
		<abnor type="COUPON" v-if="!loading&&list.data.length<=0&&list.current_page==1"></abnor>

		<view class="space-footer"></view>

	</view>
</template>

<script>
	import {
		mapState,
	} from "vuex"
	export default {
		components: {},
		data() {
			return {
				options: {},
				activeIndex: 0,
				tabList: [{
					title: '待使用',
					id: 1
				}, {
					title: '已使用',
					id: 2,
				}, {
					title: '已过期',
					id: 3
				}],
				statusType: {
					1: '去使用',
					2: '已使用',
					3: '删除',
				},
				useType: {
					is_land: '土地租赁',
					is_claim: '认养服务',
					is_shop: '商城',
				},
				param: {
					page: 1,
				},
				list: {
					data: []
				},
				loading: true,
				lockTap: false
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			userInfo: state => state.user.userInfo,
		}),
		onLoad(options) {
			this.$util.showLoading()
			this.options = options
			let {
				tab = 0
			} = options
			this.activeIndex = tab
			this.initIndex()
		},
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return
			this.param.page = this.param.page + 1
			this.loading = true
			this.getList()
		},
		methods: {

			initIndex() {
				this.getList()
			},
			initRefresh() {
				this.param.page = 1
				this.initIndex()
			},
			async getList() {
				let {
					list: oldList,
					param,
					useType,
					tabList,
					activeIndex
				} = this
				param.status = tabList[activeIndex].id
				let newList = await this.$api.mine.userCouponList(param);
				newList.data.map(item => {
					item.is_show = false
					let use_range = ''
					for (let key in useType) {
						if (item[key]) {
							use_range += `${useType[key]}、`
						}
					}
					item.use_range = use_range.substring(0, use_range.length - 1)
				})

				if (this.param.page == 1) {
					this.list = newList
				} else {
					newList.data = oldList.data.concat(newList.data)
					this.list = newList
				}
				this.loading = false
				this.$util.hideAll()
			},
			handerTabChange(index) {
				this.activeIndex = index
				this.$util.showLoading()
				this.param.page = 1
				this.list.data = []
				this.getList()
			},
			async goDetail(index) {
				let {
					activeIndex
				} = this
				switch (activeIndex) {
					case 0:
						let {
							goods, shop_goods
						} = this.list.data[index]
						let url = goods.length > 0 ? `/pages/service` : shop_goods.length > 0 ? `/pages/shop` :
							`/pages/restaurant`
						this.$util.goUrl({
							url,
							openType: 'switchTab'
						})
						break;
					case 1:
						break;
					case 2:
						let {
							id: coupon_id
						} = this.list.data[index]
						if (this.lockTap) return
						this.lockTap = true
						this.$util.showLoading()
						try {
							await this.$api.mine.couponDel({
								coupon_id
							})
							this.$util.hideAll()
							this.$util.showToast({
								title: `删除成功`
							})
							this.list.data.splice(index, 1)
							this.lockTap = false
						} catch (e) {
							setTimeout(() => {
								this.lockTap = false
								this.$util.hideAll()
							}, 2000)
						}
						break;
				}

			}
		}
	}
</script>


<style lang="scss">
	.mine-coupon-list {
		.list-item {
			overflow: hidden;

			.item-title {
				top: 0;
				left: 0;
				height: 50rpx;
				background: #FFE2E2;
				border-radius: 15rpx 0 15rpx 0;
			}

			.use-btn {
				min-width: 120rpx;
				height: 58rpx;
			}
		}
	}
</style>
